Go 连接PostgreSQL数据库

 

先在PostgreSQL数据库中建一个表,可以使用PostgreSQL官方的pgAdmin来完成:

CREATE TABLE userinfo
(
    uid serial NOT NULL,
    username character varying(100) NOT NULL,
    departname character varying(500) NOT NULL,
    Created date,
    CONSTRAINT userinfo_pkey PRIMARY KEY (uid)
)
WITH (OIDS=FALSE);

CREATE TABLE userdeatail
(
    uid integer,
    intro character varying(100),
    profile character varying(100)
)
WITH(OIDS=FALSE);

 

 


 

 

然后运行以下代码,实现连接数据库增删查改,请自行更改参数配置部分:

package main

import (
	"database/sql"
	"fmt"

	_ "github.com/lib/pq"
)

var db *sql.DB

func sqlOpen() {
	var err error
	db, err = sql.Open("postgres", "port=5433 user=postgres password=123456 dbname=ficow sslmode=disable")
	//port是数据库的端口号,默认是5432,如果改了,这里一定要自定义;
	//user就是你数据库的登录帐号;
	//dbname就是你在数据库里面建立的数据库的名字;
	//sslmode就是安全验证模式;

	//还可以是这种方式打开
	//db, err := sql.Open("postgres", "postgres://pqgotest:password@localhost/pqgotest?sslmode=verify-full")
	checkErr(err)
}
func sqlInsert() {
	//插入数据
	stmt, err := db.Prepare("INSERT INTO userinfo(username,departname,created) VALUES($1,$2,$3) RETURNING uid")
	checkErr(err)

	res, err := stmt.Exec("ficow", "软件开发部门", "2017-03-09")
	//这里的三个参数就是对应上面的$1,$2,$3了

	checkErr(err)

	affect, err := res.RowsAffected()
	checkErr(err)

	fmt.Println("rows affect:", affect)
}
func sqlDelete() {
	//删除数据
	stmt, err := db.Prepare("delete from userinfo where uid=$1")
	checkErr(err)

	res, err := stmt.Exec(1)
	checkErr(err)

	affect, err := res.RowsAffected()
	checkErr(err)

	fmt.Println("rows affect:", affect)
}
func sqlSelect() {
	//查询数据
	rows, err := db.Query("SELECT * FROM userinfo")
	checkErr(err)

	println("-----------")
	for rows.Next() {
		var uid int
		var username string
		var department string
		var created string
		err = rows.Scan(&uid, &username, &department, &created)
		checkErr(err)
		fmt.Println("uid = ", uid, "\nname = ", username, "\ndep = ", department, "\ncreated = ", created, "\n-----------")
	}
}
func sqlUpdate() {
	//更新数据
	stmt, err := db.Prepare("update userinfo set username=$1 where uid=$2")
	checkErr(err)

	res, err := stmt.Exec("ficow", 1)
	checkErr(err)

	affect, err := res.RowsAffected()
	checkErr(err)

	fmt.Println("rows affect:", affect)
}
func sqlClose() {
	db.Close()
}
func checkErr(err error) {
	if err != nil {
		panic(err)
	}
}

func sqlTest() {

	sep := "----------\n"
	sqlOpen()
	println(sep, "*sqlOpen")

	sqlSelect()
	println(sep, "*sqlSelect")

	sqlInsert()
	sqlSelect()
	println(sep, "*sqlInsert")

	sqlUpdate()
	sqlSelect()
	println(sep, "*sqlUpdate")

	sqlDelete()
	sqlSelect()
	println(sep, "*sqlDelete")

	sqlClose()
	println(sep, "*sqlClose")
}

func main() {

	sqlTest()
}

  

 

相关链接:

可以查看这个驱动库的文档进行学习: https://godoc.org/github.com/lib/pq

另外,还要感谢这位作者的教程: http://www.cnblogs.com/songxingzhu/p/5024517.html

 

 

 


 

转载请注明出处http://www.cnblogs.com/ficow/p/6537238.html ,谢谢! 

posted @ 2017-03-12 10:59  Ficow  阅读(16325)  评论(0编辑  收藏  举报